package com.pro.controller;

import com.pro.pojo.VerifyCode;
import com.pro.service.IVerifyCodeGenService;
import com.pro.service.imple.IVerifyCodeGenServiceImple;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * ClassName:VerifyCodeController
 * Package:com.pro.controller
 * Description:
 *
 * @Date:2021/12/17 14:09
 * @Author: ZPT
 */
@RestController
public class VerifyCodeController {
    @RequestMapping(value = "/verifyCode")
    public void verifyCode(HttpServletRequest request, HttpServletResponse response) {
        IVerifyCodeGenService iVerifyCodeGenService = new IVerifyCodeGenServiceImple();
        try {
            //设置长宽
            VerifyCode verifyCode = iVerifyCodeGenService.generate(80,28);
            String code = verifyCode.getCode();
            //将VerifyCode绑定session，登录控制器可从session中取出从而判断用户填的验证码是否正确
            request.getSession().setAttribute("VerifyCode", code);
            //设置响应头
            response.setHeader("Pragma", "no-cache");
            //设置响应头
            response.setHeader("Cache-Control", "no-cache");
            //在代理服务器端防止缓冲
            response.setDateHeader("Expires", 0);
            //设置响应内容类型
            response.setContentType("image/jpeg");
            response.getOutputStream().write(verifyCode.getImgBytes());
            response.getOutputStream().flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
